package com.jarchie.htgl.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.support.annotation.Nullable;

import com.jarchie.htgl.base.BaseApp;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.Map;
import java.util.Set;

/**
 * Created by Jarchie on 2017\11\7.
 * 描述：SharedPreferences存取工具类
 */

@SuppressWarnings("unused")
public class SharePreUtils {

    private SharedPreferences sp;
    private SharedPreferences.Editor editor;

    //SPUtils构造函数，在Application中初始化
    public SharePreUtils( String spName) {
        sp = BaseApp.getContext().getSharedPreferences(spName, Context.MODE_PRIVATE);
        editor = sp.edit();
        editor.apply();
    }

    //SP中写入String类型value
    public void put(String key, @Nullable String value) {
        editor.putString(key, value).apply();
    }

    //SP中读取String，存在返回对应值，不存在返回默认值{@code null}
    public String getString(String key) {
        return getString(key, null);
    }

    //SP中读取String，存在返回对应值，不存在返回默认值{@code defaultValue}
    public String getString(String key, String defaultValue) {
        return sp.getString(key, defaultValue);
    }

    //SP中写入int类型value
    public void put(String key, int value) {
        editor.putInt(key, value).apply();
    }

    //SP中读取int，存在返回对应值，不存在返回默认值-1
    public int getInt(String key) {
        return getInt(key, -1);
    }

    //SP中读取int，存在返回对应值，不存在返回默认值{@code defaultValue}
    public int getInt(String key, int defaultValue) {
        return sp.getInt(key, defaultValue);
    }

    //SP中写入long类型value
    public void put(String key, long value) {
        editor.putLong(key, value).apply();
    }

    //SP中读取long，存在返回对应值，不存在返回默认值-1
    public long getLong(String key) {
        return getLong(key, -1L);
    }

    //SP中读取long，存在返回对应值，不存在返回默认值{@code defaultValue}
    public long getLong(String key, long defaultValue) {
        return sp.getLong(key, defaultValue);
    }

    //SP中写入float类型value
    public void put(String key, float value) {
        editor.putFloat(key, value).apply();
    }

    //SP中读取float，存在返回对应值，不存在返回默认值-1
    public float getFloat(String key) {
        return getFloat(key, -1f);
    }

    //SP中读取float，存在返回对应值，不存在返回默认值{@code defaultValue}
    public float getFloat(String key, float defaultValue) {
        return sp.getFloat(key, defaultValue);
    }

    //SP中写入boolean类型value
    public void put(String key, boolean value) {
        editor.putBoolean(key, value).apply();
    }

    //SP中读取boolean，存在返回对应值，不存在返回默认值{@code false}
    public boolean getBoolean(String key) {
        return getBoolean(key, false);
    }

    //SP中读取boolean，存在返回对应值，不存在返回默认值{@code defaultValue}
    public boolean getBoolean(String key, boolean defaultValue) {
        return sp.getBoolean(key, defaultValue);
    }

    //SP中写入String集合类型value
    public void put(String key, @Nullable Set<String> values) {
        editor.putStringSet(key, values).apply();
    }

    //SP中读取StringSet，存在返回对应值，不存在返回默认值{@code null}
    public Set<String> getStringSet(String key) {
        return getStringSet(key, null);
    }

    //SP中读取StringSet，存在返回对应值，不存在返回默认值{@code defaultValue}
    public Set<String> getStringSet(String key, @Nullable Set<String> defaultValue) {
        return sp.getStringSet(key, defaultValue);
    }

    //SP中获取所有键值对
    public Map<String, ?> getAll() {
        return sp.getAll();
    }

    //SP中移除该key
    public void remove(String key) {
        editor.remove(key).apply();
    }

    //SP中是否存在该key，存在<br>{@code false}: 不存在
    public boolean contains(String key) {
        return sp.contains(key);
    }

    //SP中清除所有数据
    public void clear() {
        editor.clear().apply();
    }

    //查看SharedPreferences的内容
    public static String lookSharePre(Context context) {
        try {
            FileInputStream stream = new FileInputStream(new File("/data/data/" + context.getPackageName() + "/shared_prefs", "config.xml"));
            BufferedReader bff = new BufferedReader(new InputStreamReader(stream));
            String line;
            StringBuilder sb = new StringBuilder();
            while ((line = bff.readLine()) != null) {
                sb.append(line);
                sb.append("\n");
            }
            return sb.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "未找到当前配置文件！";
    }

}
